package com.tnovoselec.android.common.ui.splash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MotionEvent;

public abstract class SplashScreenNew extends Activity {

	public abstract int getContentViewId();

	public abstract long getDuration();

	public abstract Class<? extends Activity> getIntentClass();

	private Handler handler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(getContentViewId());

		handler.postDelayed(run, SplashScreenNew.this.getDuration());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		return false;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			handler.removeCallbacks(run);
			runOnUiThread(run);
		}

		return true;
	}

	private final Runnable run = new Runnable() {

		private boolean finished = false;

		@Override
		public void run() {
			synchronized (this) {
				if (finished) {
					return;
				}

				Intent intent = new Intent();
				intent.setClass(SplashScreenNew.this, SplashScreenNew.this.getIntentClass());
				SplashScreenNew.this.startActivity(intent);
				SplashScreenNew.this.finish();

				finished = true;
			}
		}
	};

}
